package es.onlysolutions.gs.web.controller;

import es.onlysolutions.arq.core.auth.IUserSettings;
import es.onlysolutions.arq.core.mvc.controller.GenericController;
import es.onlysolutions.gs.comm.entity.User;
import es.onlysolutions.gs.comm.utils.ExceptionHandler;
import es.onlysolutions.gs.comm.utils.UserSettings;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Abstract class for all controllers.
 */
public abstract class GSController extends GenericController
{


    protected GSController()
    {
        setIeConfiguration( new ExceptionHandler() );
    }

    /**
     * Extract the user from the session.
     * @param request The HttpServletRequest to get the session.
     * @return The User or null if it cannot be found.
     */
    protected User getUserFromSession( HttpServletRequest request )
    {
        User result = null;
        HttpSession session = request.getSession(false);
        if( session != null )
        {
            UserSettings userSettings = (UserSettings) session.getAttribute(IUserSettings.USER_SETTINGS_ATTRIBUTE_NAME);
            if( userSettings != null )
            {
                result = userSettings.getUser();
            }
        }

        return result;
    }
}
